// Created by WXX on 2021/10/5 13:35
#include <iostream>

using namespace std;

class Solution {
public:
    string reverseOnlyLetters(string s) {
        for (int i = 0, j = s.size() - 1; i < j; i++, j--) {
            while (i < j && !is_letter(s[i])) i++;
            while (i < j && !is_letter(s[j])) j--;
            if (i != j) swap(s[i], s[j]);
        }
        return s;
    }

    bool is_letter(char c) {
        return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z';
    }
};

int main() {

    cout << Solution().reverseOnlyLetters("ab-cd") << endl;  // dc-ba

    return 0;
}
